//Accepted
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	//freopen("in.txt", "r", stdin);
	int  n, k, i, dif, t, a[20000];
	scanf("%d", &t);
	while(t--){
		scanf("%d%d", &n,&k);
		for(i=0;i<n;++i)
			scanf("%d", &a[i]);
		
		sort(a, a+n);
		dif=2e9;
		for(i=k-1;i<n;++i)
			if(a[i]-a[i-k+1]<dif)dif=a[i]-a[i-k+1];
		
		printf("%d\n", dif);
	}
	
	return 0;
}